var dot_inter,bool;
var interval;
var $ = getApp(), methods = require("../methods.js");
var windowWidth = wx.getSystemInfoSync().windowWidth
Page(Object.assign({}, methods, {
	data:{
    isStart: 1,
    name: "",
    jxmc: "",
    detect: 1,
    error: "",
    remaindaytimes: 0,
    register: 1,
    award_name: 0,
		jxshow:false,
		showmaskrule:false
	},
	_onLoad: function () {
		var that = this;
		var id = '';
		if (that.options && that.options.id) {
			id = that.options.id
		}
		if (that.options && that.options.scene) {
			var scene = that.options.scene
			var scenes = scene.split('_');
			if(scenes[1]){
				id = parseInt(scenes[1]);
			}
		}
		$.get('ggk/index',{id:id},function(res){
			that.setData(res)
			wx.setNavigationBarTitle({
				title: res.info.name
			});
			that.init();
			
			if(that.data.info.fanwei==1){
				wx.getLocation({
					type: 'gcj02',
					success:function(res){
						var latitude = res.latitude
						var longitude = res.longitude
						that.setData({latitude:latitude,longitude:longitude})
					}
				})
			}
		})
	},
	changemaskrule:function(){
		this.setData({showmaskrule:!this.data.showmaskrule})
	},
	//changemask:function(){
	//	this.setData({jxshow:!this.data.jxshow})
	//	this.reLoad();
	//},
	onShareAppMessage: function () {
		var that = this
		var thisurl = $._fullurl();
		if(this.options.scene){
			var scene = this.options.scene
			var scenes = scene.split('_');
			thisurl += '?scene=' + $.mid + (scenes[1] ? '_' + scenes[1] :'') + (scenes[2] ? '_' + scenes[2] :'');
		}else{
			if (thisurl.indexOf('?') > 0) {
				thisurl += '&scene=' + $.mid
			} else {
				thisurl += '?scene=' + $.mid
			}
		}
		//console.log(thisurl);
		var title = that.data.info.name;
		if(that.data.info.sharetitle){
			title = that.data.info.sharetitle;
		}
		$.post("ggk/share",{ hid:that.data.info.id },function(res){
			if(res.status==1){
				setTimeout(function(){
					that.reLoad()
				},1000);
			}else if(res.status==0){
				//dialog(res.msg);
			}
		});
		return {
			title: title,
			imageUrl: that.data.info.sharepic ? that.data.info.sharepic:'',
			path: thisurl
		}
	},

  init: function () {
    var query = wx.createSelectorQuery();
    var that = this;
    that.setData({
      award_name: !1
    });
		query.select("#frame").boundingClientRect();
		query.exec(function (res) {
      var width = res[0].width;
      var height = res[0].height;
      that.setData({r: 16,lastX: "",lastY: "",minX: "",minY: "",maxX: "",maxY: "",canvasWidth: width,canvasHeight: height});
      var scratch = wx.createCanvasContext("scratch");
			//scratch.setFillStyle('#D3D3D3')
			//scratch.fillRect(0, 0, width, height)
			//scratch.draw()
      scratch.drawImage("/images/scratch_hide_2.png", 0, 0, width, height);
			scratch.draw();
			that.ctx = scratch;
			that.setData({
        isStart: 1,
        isScroll: 1
      });
    })
  },
	onStart: function () {
		console.log('11');
    this.setData({
			jxmc:'',
      isStart: 1,
      award: 0,
      award_name: 0
    });
		this.init()
  },
  drawRect: function (t, e) {
    var a = this.data.r / 2,
      i = 0 < t - a ? t - a : 0,
      s = 0 < e - a ? e - a : 0;
    return "" !== this.data.minX ? this.setData({
      minX: this.data.minX > i ? i : this.data.minX,
      minY: this.data.minY > s ? s : this.data.minY,
      maxX: this.data.maxX > i ? this.data.maxX : i,
      maxY: this.data.maxY > s ? this.data.maxY : s
    }) : this.setData({
      minX: i,
      minY: s,
      maxX: i,
      maxY: s
    }), this.setData({
      lastX: i,
      lastY: s
    }), [i, s, 2 * a]
  },
  clearArc: function (x, y, a) {
    var r = this.data.r;
    var ctx = this.ctx;
    var x2 = r - a;
    var y2 = Math.sqrt(r * r - x2 * x2);
    var c = x - x2;
    var n = y - y2;
    var d = 2 * x2;
    var p = 2 * y2;
    if(a <= r){
			ctx.clearRect(c, n, d, p);
			a += 1;
			this.clearArc(x, y, a);
		}
  },
  touchStart: function (t) {
		this.setData({
			award_name: 1
    })
    if (this.data.isStart && this.data.error){
			$.alert(this.data.error)
		}
  },
  touchMove: function (t) {
    if (this.data.isStart && !this.data.error) {
      this.drawRect(t.touches[0].x, t.touches[0].y);
			this.clearArc(t.touches[0].x, t.touches[0].y, 1);
			this.ctx.draw(true)
    }
  },
  touchEnd: function (t) {
    if (this.data.isStart && !this.data.error){
      var that = this;
      var canvasWidth = this.data.canvasWidth;
      var canvasHeight = this.data.canvasHeight;
      var minX = this.data.minX;
      var minY = this.data.minY;
      var maxX = this.data.maxX;
      var maxY = this.data.maxY;
			if(0.4 * canvasWidth < maxX - minX && 0.4 * canvasHeight < maxY - minY && this.data.detect){
				that.setData({
					detect: 0
				});
				$.post('ggk/index',{id:that.data.info.id,op:'getjx',longitude:that.data.longitude,latitude:that.data.latitude},function(res){
					if(res.status!=1){
						$.alert(res.msg);
						that.onStart();
					}else{
						that.setData({jxmc:res.jxmc,jx:res.jx,remaindaytimes:that.data.remaindaytimes-1});
						setTimeout(function(){
							that.setData({
								detect:1,
								isStart: 0,
								isScroll: 1
							})
						},1000)
						if(res.jxtp == 2 && res.spdata){
							wx.sendBizRedPacket({
								timeStamp: res.spdata.timeStamp, // 支付签名时间戳，
								nonceStr: res.spdata.nonceStr, // 支付签名随机串，不长于 32 位
								package: res.spdata.package, //扩展字段，由商户传入
								signType: res.spdata.signType, // 签名方式，
								paySign: res.spdata.paySign, // 支付签名
								success:function(res){
									console.log(res);
								},
								fail:function(res){
									console.log(res);
								},
								complete:function(res){
									console.log(res);
								}
							})
						}
					}
				})
			}
    }
  },
}))